#include "stdafx.h"

	namespace network
	{
		namespace helper
		{


		MulticastReceiverHelper::MulticastReceiverHelper(	const boost::asio::ip::address& listen_address,
															const boost::asio::ip::address& multicast_address) :
			m_ioService(),
			m_socket(m_ioService ),
			m_data( new std::vector< unsigned char >( max_length ) )
		{
			// Create the socket so that multiple may be bound to the same address.
			boost::asio::ip::udp::endpoint listen_endpoint(listen_address, multicast_port);
			m_socket.open(listen_endpoint.protocol());
			m_socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));

			m_socket.bind(listen_endpoint);

			// Join the multicast group.
			m_socket.set_option( boost::asio::ip::multicast::join_group(multicast_address) );
		}

		MulticastReceiverHelper::~MulticastReceiverHelper()
		{
		}


		void MulticastReceiverHelper::handle_receive_from( const boost::system::error_code& error, size_t bytes_recvd)
		{
			if (!error)
			{
#if defined(_DEBUG) && defined(_OUTPUT)
				std::cout << "Receive Multicast request from : " << m_senderEndpoint << std::endl;
#endif
			}
		}

		void MulticastReceiverHelper::close()
		{
			m_ioService.post( boost::bind(&MulticastReceiverHelper::handle_stop, this) );	
			m_socket.close();
		}

		void MulticastReceiverHelper::handle_stop()
		{
#if defined(_DEBUG) && defined(_OUTPUT)
			std::cout << " [ MulticastReceiverHelper ]" << std::endl;
			std::cout << " stop I/O services on multicast receiver " << std::endl;
#endif
		}

	}	//	namespace helper
}	//	//	namespace network